#!/bin/bash

path=`pwd`
if [ "${path##*/}"x = "notes"x ] 
then

    # 私有数据备份
    rm -rd ../private
    rm -rd ../.obsidian
    mv ./content/private ../
    mv ./content/.obsidian ../

    # 生成待发布的数据
    ./forgitee
    
    # 数据备份
    rm -rd ../public
    mv public ../
    cp .gitignore ../public/
    cp publish ../

    # 切换到发布分支
    git add .
    git commit -m "忘提交则补提交"
    git checkout gh-pages

    # # 重新执行
    cd ..

    if [ "$1"x = "force"x ] 
    then
        ./publish force
    else
        ./publish
    fi
elif  [ -d "./notes" ]    
then
    cd notes

    if [ "$1"x = "force"x ] 
    then
        # 清空库
	git checkout --orphan gh-pages-bak
	git branch -D gh-pages
    fi

    rm -rf *

    # 数据提交
    cp -r  ../public/*  ./
    git add .

    # 提交并发布
    if [ "$1"x = "force"x ] 
    then
        git commit -m "发布(并且清空历史记录)"
        git branch -m gh-pages
        git push gitee gh-pages --force
        git push github gh-pages --force
    else
        git commit -m "发布"
        git push gitee gh-pages
        git push github gh-pages
    fi

    # 复原
    git checkout master
    rm -rf  ../public
    rm ../publish

    mv ../private ./content/ 
    mv ../.obsidian ./content/
else
    echo "此脚本只能在特定目录（notes目录或其父目录）中执行"
fi

